﻿using System;
using System.Windows.Input;

namespace SnakeGame.ViewModels
{
    class RelayCommand : ICommand
    {
        private readonly Action<object> _action;
        private readonly Func<object, bool> _canExecuteAction;

        public RelayCommand(Action<object> action)
        {
            _action = action;
        }

        public RelayCommand(Action<object> action, Func<object, bool> canExecuteAction)
        {
            _action = action;
            _canExecuteAction = canExecuteAction;
        }

        public void Execute(object parameter)
        {
            _action(parameter);
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecuteAction != null)
            {
                return _canExecuteAction(parameter);
            }
            else
            {
                return true;
            }
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
